﻿using Newtonsoft.Json;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;
using Org.BouncyCastle.X509;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Syspetro.Core.Encrypt
{
    public class RSAKeyParameter
    {
        public string PrivateKey { get; set; }
        public string PublicKey { get; set; }
    }
    public enum PKCS
    {
        PKCS1 = 0,
        PKCS8 = 1
    }
    public enum Decode
    {
        Base64 = 0,
        Hex = 1
    }
    public class CipherAlgorithms
    {
        public const string RSA_NONE_NoPadding = "RSA/NONE/NoPadding";

        public const string RSA_NONE_PKCS1Padding = "RSA/NONE/PKCS1Padding";

        public const string RSA_NONE_OAEPPadding = "RSA/NONE/OAEPPadding";

        public const string RSA_ECB_NoPadding = "RSA/ECB/NoPadding";

        public const string RSA_ECB_PKCS1Padding = "RSA/ECB/PKCS1Padding";

        public const string RSA_ECB_OAEPPadding = "RSA/ECB/OAEPPadding";
    }
    public class RSAHelper
    {
        private const string privatePkcs1Key = "pkcs1key.json";
        private const string publicPkcs1Key = "pkcs1key.public.json";

        private const string privatePkcs8Key = "pkcs8key.json";
        private const string publicPkcs8Key = "pkcs8key.public.json";

        private static RSAKeyParameter keyParameter1 = null;
        private static RSAKeyParameter keyParameter8 = null;

        #region 生产公钥密钥
        /// <summary>
        /// 生成密钥和公钥并保存到本地
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="pKCS"></param>
        /// <param name="keySize"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static RSAKeyParameter GetRSAKeyParameter(string filePath, PKCS pKCS = PKCS.PKCS1, int keySize = 1024, bool format = false)
        {
            if (pKCS == PKCS.PKCS1)
            {
                if (keyParameter1 != null)
                {
                    return keyParameter1;
                }
                var filenamePrivate = Path.Combine(filePath, privatePkcs1Key);
                var filenamePublice = Path.Combine(filePath, publicPkcs1Key);
                if (File.Exists(filenamePublice))
                    return ReadRSAKeyParameter(pKCS, filenamePrivate, filenamePublice);
                else
                {
                    keyParameter1 = Pkcs1(keySize, format);
                    WriteRSAKeyParameter(keyParameter1, filenamePrivate, filenamePublice);
                    return keyParameter1;
                }
            }
            else
            {
                if (keyParameter8 != null)
                {
                    return keyParameter8;
                }
                var filenamePrivate = Path.Combine(filePath, privatePkcs8Key);
                var filenamePublice = Path.Combine(filePath, publicPkcs8Key);
                if (File.Exists(filenamePublice))
                    return ReadRSAKeyParameter(pKCS, filenamePrivate, filenamePublice);
                else
                {
                    keyParameter8 = Pkcs8(keySize, format);
                    WriteRSAKeyParameter(keyParameter8, filenamePrivate, filenamePublice);
                    return keyParameter8;
                }
            }
        }
        /// <summary>
        /// 生成RSA秘钥(PKCS1格式)
        /// </summary>
        /// <param name="keySize"></param>
        /// <param name="format">PEM格式</param>
        /// <returns></returns>
        public static RSAKeyParameter Pkcs1(int keySize = 1024, bool format = false)
        {
            var keyGenerator = GeneratorUtilities.GetKeyPairGenerator("RSA");
            keyGenerator.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
            var keyPair = keyGenerator.GenerateKeyPair();
            var subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
            var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private);
            if (!format)
            {
                return new RSAKeyParameter
                {
                    PrivateKey = Base64.ToBase64String(privateKeyInfo.ParsePrivateKey().GetEncoded()),
                    PublicKey = Base64.ToBase64String(subjectPublicKeyInfo.GetEncoded())
                };
            }
            var rsaKey = new RSAKeyParameter();
            using (var sw = new StringWriter())
            {
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(keyPair.Private);
                pWrt.Writer.Close();
                rsaKey.PrivateKey = sw.ToString();
            }
            using (var sw = new StringWriter())
            {
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(keyPair.Public);
                pWrt.Writer.Close();
                rsaKey.PublicKey = sw.ToString();
            }
            return rsaKey;
        }
        /// <summary>
        /// 从Pkcs1私钥中提取公钥
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string GetPublicKeyFromPrivateKeyPkcs1(string privateKey)
        {
            var instance = RsaPrivateKeyStructure.GetInstance(Base64.Decode(privateKey));
            var publicParameter = (AsymmetricKeyParameter)new RsaKeyParameters(false, instance.Modulus, instance.PublicExponent);
            var privateParameter = (AsymmetricKeyParameter)new RsaPrivateCrtKeyParameters(instance.Modulus, instance.PublicExponent, instance.PrivateExponent, instance.Prime1, instance.Prime2, instance.Exponent1, instance.Exponent2, instance.Coefficient);
            var keyPair = new AsymmetricCipherKeyPair(publicParameter, privateParameter);
            var subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
            return Base64.ToBase64String(subjectPublicKeyInfo.GetEncoded());
        }
        /// <summary>
        /// 生成RSA秘钥(PKCS8格式)
        /// </summary>
        /// <param name="keySize"></param>
        /// <param name="format">PEM格式</param>
        /// <returns></returns>
        public static RSAKeyParameter Pkcs8(int keySize = 1024, bool format = false)
        {
            var keyGenerator = GeneratorUtilities.GetKeyPairGenerator("RSA");
            keyGenerator.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
            var keyPair = keyGenerator.GenerateKeyPair();
            var subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
            var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private);
            if (!format)
            {
                return new RSAKeyParameter
                {
                    PrivateKey = Base64.ToBase64String(privateKeyInfo.GetEncoded()),
                    PublicKey = Base64.ToBase64String(subjectPublicKeyInfo.GetEncoded())
                };
            }
            var rsaKey = new RSAKeyParameter();
            using (var sw = new StringWriter())
            {
                var pWrt = new PemWriter(sw);
                var pkcs8 = new Pkcs8Generator(keyPair.Private);
                pWrt.WriteObject(pkcs8);
                pWrt.Writer.Close();
                rsaKey.PrivateKey = sw.ToString();
            }
            using (var sw = new StringWriter())
            {
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(keyPair.Public);
                pWrt.Writer.Close();
                rsaKey.PublicKey = sw.ToString();
            }
            return rsaKey;
        }
        /// <summary>
        /// 从Pkcs8私钥中提取公钥
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string GetPublicKeyFromPrivateKeyPkcs8(string privateKey)
        {
            var privateKeyInfo = PrivateKeyInfo.GetInstance(Asn1Object.FromByteArray(Base64.Decode(privateKey)));
            privateKey = Base64.ToBase64String(privateKeyInfo.ParsePrivateKey().GetEncoded());
            var instance = RsaPrivateKeyStructure.GetInstance(Base64.Decode(privateKey));
            var publicParameter = (AsymmetricKeyParameter)new RsaKeyParameters(false, instance.Modulus, instance.PublicExponent);
            var privateParameter = (AsymmetricKeyParameter)new RsaPrivateCrtKeyParameters(instance.Modulus, instance.PublicExponent, instance.PrivateExponent, instance.Prime1, instance.Prime2, instance.Exponent1, instance.Exponent2, instance.Coefficient);
            var keyPair = new AsymmetricCipherKeyPair(publicParameter, privateParameter);
            var subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
            return Base64.ToBase64String(subjectPublicKeyInfo.GetEncoded());
        }
        #endregion

        #region PEM证书格式密钥读取
        /// <summary>
        /// PEM证书Pkcs1密钥读取
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ReadPkcs1PrivateKey(string text)
        {
            if (!text.StartsWith("-----BEGIN RSA PRIVATE KEY-----"))
            {
                return text;
            }
            using var reader = new StringReader(text);
            var pr = new PemReader(reader);
            var keyPair = pr.ReadObject() as AsymmetricCipherKeyPair;
            pr.Reader.Close();
            var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair?.Private);
            return Base64.ToBase64String(privateKeyInfo.ParsePrivateKey().GetEncoded());
        }
        /// <summary>
        /// PEM证书Pkcs8密钥读取
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ReadPkcs8PrivateKey(string text)
        {
            if (!text.StartsWith("-----BEGIN PRIVATE KEY-----"))
            {
                return text;
            }
            using var reader = new StringReader(text);
            var pr = new PemReader(reader);
            var akp = pr.ReadObject() as AsymmetricKeyParameter; ;
            pr.Reader.Close();
            return Base64.ToBase64String(PrivateKeyInfoFactory.CreatePrivateKeyInfo(akp).GetEncoded());
        }
        /// <summary>
        /// PEM证书公钥读取
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ReadPublicKey(string text)
        {
            if (!text.StartsWith("-----BEGIN PUBLIC KEY-----"))
            {
                return text;
            }
            using var reader = new StringReader(text);
            var pr = new PemReader(reader);
            var keyPair = pr.ReadObject() as AsymmetricCipherKeyPair;
            pr.Reader.Close();
            var subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair?.Public);
            return Base64.ToBase64String(subjectPublicKeyInfo.GetEncoded());
        }
        #endregion

        #region PEM证书格式密钥写入
        /// <summary>
        /// PEM证书Pkcs1密钥写入
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string WritePkcs1PrivateKey(string privateKey)
        {
            if (privateKey.StartsWith("-----BEGIN RSA PRIVATE KEY-----"))
            {
                return privateKey;
            }
            var akp = GetAsymmetricKeyParameterFormPkcs1PrivateKey(privateKey);
            using var sw = new StringWriter();
            var pWrt = new PemWriter(sw);
            pWrt.WriteObject(akp);
            pWrt.Writer.Close();
            return sw.ToString();
        }
        /// <summary>
        /// PEM证书Pkcs8密钥写入
        /// </summary>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        public static string WritePkcs8PrivateKey(string privateKey)
        {
            if (privateKey.StartsWith("-----BEGIN PRIVATE KEY-----"))
            {
                return privateKey;
            }
            var akp = GetAsymmetricKeyParameterFormPkcs8PrivateKey(privateKey);
            using var sw = new StringWriter();
            var pWrt = new PemWriter(sw);
            var pkcs8 = new Pkcs8Generator(akp);
            pWrt.WriteObject(pkcs8);
            pWrt.Writer.Close();
            return sw.ToString();
        }
        /// <summary>
        /// PEM证书公钥写入
        /// </summary>
        /// <param name="publicKey"></param>
        /// <returns></returns>
        public static string WritePublicKey(string publicKey)
        {
            if (publicKey.StartsWith("-----BEGIN PUBLIC KEY-----"))
            {
                return publicKey;
            }
            var akp = GetAsymmetricKeyParameterFormPublicKey(publicKey);
            using var sw = new StringWriter();
            var pWrt = new PemWriter(sw);
            pWrt.WriteObject(akp);
            pWrt.Writer.Close();
            return sw.ToString();
        }
        #endregion


        #region 格式化参数
        /// <summary>
        /// 私钥格式转换(Pkcs1>>Pkcs8)
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string PrivateKeyPkcs1ToPkcs8(string privateKey, bool format = false)
        {
            var akp = GetAsymmetricKeyParameterFormPkcs1PrivateKey(privateKey);
            if (format)
            {
                var sw = new StringWriter();
                var pWrt = new PemWriter(sw);
                var pkcs8 = new Pkcs8Generator(akp);
                pWrt.WriteObject(pkcs8);
                pWrt.Writer.Close();
                return sw.ToString();
            }
            else
            {
                var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(akp);
                return Base64.ToBase64String(privateKeyInfo.GetEncoded());
            }
        }
        /// <summary>
        /// 私钥格式转换(Pkcs8>>Pkcs1)
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string PrivateKeyPkcs8ToPkcs1(string privateKey, bool format = false)
        {
            var akp = GetAsymmetricKeyParameterFormPkcs8PrivateKey(privateKey);
            if (format)
            {
                var sw = new StringWriter();
                var pWrt = new PemWriter(sw);
                pWrt.WriteObject(akp);
                pWrt.Writer.Close();
                return sw.ToString();
            }
            else
            {
                var privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(akp);
                return Base64.ToBase64String(privateKeyInfo.ParsePrivateKey().GetEncoded());
            }
        }
        /// <summary>
        /// 获取非对称秘钥参数
        /// </summary>
        /// <param name="privateKey">Pkcs1格式私钥</param>
        /// <returns></returns>
        private static AsymmetricKeyParameter GetAsymmetricKeyParameterFormPkcs1PrivateKey(string privateKey)
        {
            if (string.IsNullOrEmpty(privateKey))
            {
                throw new ArgumentNullException(nameof(privateKey));
            }
            var instance = RsaPrivateKeyStructure.GetInstance(Base64.Decode(privateKey));
            return new RsaPrivateCrtKeyParameters(instance.Modulus, instance.PublicExponent, instance.PrivateExponent, instance.Prime1, instance.Prime2, instance.Exponent1, instance.Exponent2, instance.Coefficient);
        }
        /// <summary>
        /// 获取非对称秘钥参数
        /// </summary>
        /// <param name="privateKey">Pkcs8格式私钥</param>
        /// <returns></returns>
        private static AsymmetricKeyParameter GetAsymmetricKeyParameterFormPkcs8PrivateKey(string privateKey)
        {

            return PrivateKeyFactory.CreateKey(Base64.Decode(privateKey));
        }
        /// <summary>
        /// 获取非对称秘钥参数
        /// </summary>
        /// <param name="publicKey">公钥</param>
        /// <returns></returns>
        private static AsymmetricKeyParameter GetAsymmetricKeyParameterFormPublicKey(string publicKey)
        {
            if (string.IsNullOrEmpty(publicKey))
            {
                throw new ArgumentNullException(nameof(publicKey));
            }
            return PublicKeyFactory.CreateKey(Base64.Decode(publicKey));
        }
        #endregion

        #region 加密解密
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pKCS"></param>
        /// <param name="decode"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        public static string Encrypt(string data, PKCS pKCS = PKCS.PKCS1, Decode decode = Decode.Base64, string algorithm = null)
        {
            try
            {
                string keyDir = Directory.GetCurrentDirectory();
                var keys = GetRSAKeyParameter(keyDir, pKCS);
                return Encrypt(data, keys.PrivateKey, pKCS, decode, algorithm);
            }
            catch
            {
                return data;
            }
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="pKCS"></param>
        /// <param name="decode"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        public static string Decrypt(string data, PKCS pKCS = PKCS.PKCS1, Decode decode = Decode.Base64, string algorithm = null)
        {
            try
            {
                string keyDir = Directory.GetCurrentDirectory();
                var keys = GetRSAKeyParameter(keyDir, pKCS);
                return Decrypt(data, keys.PublicKey, decode, algorithm);
            }
            catch
            {
                return data;
            }
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="privateKey"></param>
        /// <param name="pKCS"></param>
        /// <param name="decode"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        public static string Encrypt(string data, string privateKey, PKCS pKCS = PKCS.PKCS1, Decode decode = Decode.Base64, string algorithm = null)
        {
            AsymmetricKeyParameter parameter;
            if (pKCS == PKCS.PKCS1)
            {
                parameter = GetAsymmetricKeyParameterFormPkcs1PrivateKey(privateKey);
            }
            else
            {
                parameter = GetAsymmetricKeyParameterFormPkcs8PrivateKey(privateKey);
            }
            if (decode == Decode.Base64)
            {
                return EncryptToBase64(data, parameter, algorithm);
            }
            else
            {
                return EncryptToHex(data, parameter, algorithm);
            }
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="publicKey"></param>
        /// <param name="decode"></param>
        /// <param name="algorithm"></param>
        /// <returns></returns>
        public static string Decrypt(string data, string publicKey, Decode decode = Decode.Base64, string algorithm = null)
        {
            AsymmetricKeyParameter parameter = GetAsymmetricKeyParameterFormPublicKey(publicKey);
            if (decode == Decode.Base64)
            {
                return DecryptFromBase64(data, parameter, algorithm);
            }
            else
            {
                return DecryptFromHex(data, parameter, algorithm);
            }
        }

        /// <summary>
        /// RSA加密——Base64
        /// </summary>
        /// <param name="data">未加密数据字节数组</param>
        /// <param name="parameters">非对称密钥参数</param>
        /// <param name="algorithm">密文算法</param>
        /// <returns></returns>
        public static string EncryptToBase64(string data, AsymmetricKeyParameter parameters, string algorithm = null)
        {
            if (string.IsNullOrEmpty(algorithm)) algorithm = CipherAlgorithms.RSA_ECB_NoPadding;
            return Base64.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(data), parameters, algorithm));
        }
        /// <summary>
        /// RSA解密——Base64
        /// </summary>
        /// <param name="data">已加密数据字节数组</param>
        /// <param name="parameters">非对称密钥参数</param>
        /// <param name="algorithm">密文算法</param>
        /// <returns></returns>
        public static string DecryptFromBase64(string data, AsymmetricKeyParameter parameters, string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm)) algorithm = CipherAlgorithms.RSA_ECB_NoPadding;
            return Encoding.UTF8.GetString(Decrypt(Base64.Decode(data), parameters, algorithm));
        }

        /// <summary>
        /// RSA加密——十六进制
        /// </summary>
        /// <param name="data">未加密数据字节数组</param>
        /// <param name="parameters">非对称密钥参数</param>
        /// <param name="algorithm">密文算法</param>
        /// <returns></returns>
        public static string EncryptToHex(string data, AsymmetricKeyParameter parameters, string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm)) algorithm = CipherAlgorithms.RSA_ECB_NoPadding;
            return Hex.ToHexString(Encrypt(Encoding.UTF8.GetBytes(data), parameters, algorithm));
        }
        /// <summary>
        /// RSA解密——十六进制
        /// </summary>
        /// <param name="data">已加密数据字节数组</param>
        /// <param name="parameters">非对称密钥参数</param>
        /// <param name="algorithm">密文算法</param>
        /// <returns></returns>
        public static string DecryptFromHex(string data, AsymmetricKeyParameter parameters, string algorithm)
        {
            if (string.IsNullOrEmpty(algorithm)) algorithm = CipherAlgorithms.RSA_ECB_NoPadding;
            return Encoding.UTF8.GetString(Decrypt(Hex.Decode(data), parameters, algorithm));
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data">未加密数据字节数组</param>
        /// <param name="parameters">非对称密钥参数</param>
        /// <param name="algorithm">密文算法</param>
        /// <returns></returns>
        private static byte[] Encrypt(byte[] data, AsymmetricKeyParameter parameters, string algorithm)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (string.IsNullOrEmpty(algorithm))
            {
                throw new ArgumentNullException(nameof(algorithm));
            }
            var bufferedCipher = CipherUtilities.GetCipher(algorithm);
            bufferedCipher.Init(true, parameters);
            return bufferedCipher.DoFinal(data);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data">已加密数据字节数组</param>
        /// <param name="parameters">非对称密钥参数</param>
        /// <param name="algorithm">密文算法</param>
        /// <returns></returns>
        private static byte[] Decrypt(byte[] data, AsymmetricKeyParameter parameters, string algorithm)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (parameters == null)

            {
                throw new ArgumentNullException(nameof(parameters));
            }
            if (string.IsNullOrEmpty(algorithm))

            {
                throw new ArgumentNullException(nameof(algorithm));
            }
            var bufferedCipher = CipherUtilities.GetCipher(algorithm);
            bufferedCipher.Init(false, parameters);
            return bufferedCipher.DoFinal(data);
        }
        #endregion

        private static RSAKeyParameter ReadRSAKeyParameter(PKCS pKCS, string filenamePrivate, string filenamePublice)
        {
            if (pKCS == PKCS.PKCS1)
            {
                keyParameter1 = new RSAKeyParameter();
                if (File.Exists(filenamePrivate))
                    keyParameter1.PrivateKey = File.ReadAllText(filenamePrivate);
                if (File.Exists(filenamePublice))
                    keyParameter1.PublicKey = File.ReadAllText(filenamePublice);
                else if (!string.IsNullOrEmpty(keyParameter1.PrivateKey))
                {
                    keyParameter1.PublicKey = GetPublicKeyFromPrivateKeyPkcs1(keyParameter1.PrivateKey);
                }
                return keyParameter1;
            }
            else
            {
                keyParameter8 = new RSAKeyParameter();
                if (File.Exists(filenamePrivate))
                    keyParameter8.PrivateKey = File.ReadAllText(filenamePrivate);
                if (File.Exists(filenamePublice))
                    keyParameter8.PublicKey = File.ReadAllText(filenamePublice);
                else if (!string.IsNullOrEmpty(keyParameter8.PrivateKey))
                {
                    keyParameter8.PublicKey = GetPublicKeyFromPrivateKeyPkcs8(keyParameter8.PrivateKey);
                }
                return keyParameter8;
            }
        }
        private static void WriteRSAKeyParameter(RSAKeyParameter rSAKey, string filenamePrivate, string filenamePublice)
        {
            File.WriteAllText(filenamePrivate, rSAKey.PrivateKey);
            File.WriteAllText(filenamePublice, rSAKey.PublicKey);
        }
    }
}
